<template>
  <div>
    <h1>商品管理</h1>
    <!-- 查询 -->
    <div>
      <el-form :inline="true" :model="searchData" class="demo-form-inline">
        <el-form-item label="品牌名称">
          <el-input
            v-model="searchData.keyWord"
            placeholder="品牌名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 列表 -->
    <el-button type="success" @click="addb()">添加</el-button>
    <el-button type="danger" @click="deleteBrandAll()">批量删除</el-button>
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="brandId" label="Id" />
      <el-table-column prop="brandName" label="品牌名称" />
      <el-table-column prop="brandUse" label="使用商品数量" />
      <el-table-column prop="brandTop" label="排序" />
      <el-table-column prop="brandStatus" label="状态">
        <template #default="scope">
          <el-switch
            v-model="scope.row.brandStatus"
            size="large"
            :active-value="0"
            :inactive-value="1"
            @change="updateStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建人" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="updateBy" label="修改人" />
      <el-table-column prop="updateTime" label="修改时间" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="danger" @click="deleteBrand(scope.row)" size="small"
            >删除</el-button
          >
          <el-button type="primary" @click="seeBrand(scope.row)" size="small"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="searchData.pageNum"
        v-model:page-size="searchData.pageSize"
        :page-sizes="[2, 3, 4, 5]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-dialog v-model="dialogFormVisible" title="添加品牌" width="500">
        <el-form :model="form" :rules="rules" ref="ruleFormRef">
          <el-form-item
            label="品牌名称"
            :label-width="formLabelWidth"
            prop="brandName"
          >
            <el-input v-model="form.brandName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="排序"
            :label-width="formLabelWidth"
            prop="brandTop"
          >
            <el-input-number v-model="form.brandTop" :min="1" />
          </el-form-item>
          <el-form-item
            label="状态"
            :label-width="formLabelWidth"
            prop="brandStatus"
          >
            <el-radio-group v-model="form.brandStatus">
              <el-radio value="0" size="large">显示</el-radio>
              <el-radio value="1" size="large">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              确定
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import {
  loginService,
  deleteBrandService,
  deleteBrandAllService,
} from "../api/api";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { ElMessageBox } from "element-plus";
import {
  getBrandListService,
  addBrandService,
  updateStatusService,
} from "../api/api";
const ruleFormRef = ref<FormInstance>();
const router = useRouter();
const tableData = ref([]);
const total = ref(0);
const searchData = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: null,
});
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchData.value.pageSize = val;
  getBrandList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchData.value.pageNum = val;
  getBrandList();
};
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const form = ref({
  brandName: "",
  brandStatus: null,
  brandTop: null,
});
const rules = ref<FormRules>({
  brandName: [{ required: true, message: "商品名不能为空", trigger: "blur" }],
  brandStatus: [{ required: true, message: "状态不能为空", trigger: "blur" }],
  brandTop: [{ required: true, message: "排序不能为空", trigger: "blur" }],
});
//修改状态
const updateStatus = (row: any) => {
  updateStatusService(row).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      getBrandList();
    } else {
      ElMessage.error("修改失败");
    }
  });
};
//删除
const deleteBrand = (row: any) => {
  ElMessageBox.confirm("是否确认删除?", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteBrandService(row.brandId).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          getBrandList();
        } else {
          ElMessage.error("删除失败");
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消成功",
      });
    });
};
//批量删除
const multipleSelection = ref<any[]>([]);
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
};
const deleteBrandAll = () => {
  ElMessageBox.confirm("是否确认删除?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteBrandAllService(multipleSelection.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          getBrandList();
        } else {
          ElMessage.error("删除失败");
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消",
      });
    });
};
//编辑
const seeBrand = (row: any) => {};
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      addBrandService(form.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success(res.data.msg);
          dialogFormVisible.value = false;
          getBrandList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
//查询
const onSubmit = () => {
  console.log("submit!");
  getBrandList();
};
const getBrandList = () => {
  getBrandListService(searchData.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
const addb = () => {
  form.value = { brandName: null, brandStatus: null, brandTop: null };
  dialogFormVisible.value = true;
};
onMounted(() => {
  getBrandList();
});
</script>
